package com.example.myproject.zebra.sdk;

import com.zebra.sdk.graphics.ZebraImageFactory;
import com.zebra.sdk.graphics.ZebraImageI;
import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;

public class SdkUse2 {
    private static Logger log = LoggerFactory.getLogger(SdkUse2.class);

    public  static void fun1(ZebraPrinter zebraPrinter) throws  Exception{
        PrinterStatus printerStatus = null; //zebraPrinter.getCurrentStatus();
        //判断打印机目前是否能进行正常打印，如果不正常，循环睡眠至可打印为止
        log.info("是否打印就绪:" + printerStatus.isReadyToPrint);
        log.info("打印机缓存区是否已满:" + printerStatus.isReceiveBufferFull);
        log.info("当前缓存区数量:" + printerStatus.numberOfFormatsInReceiveBuffer);
        log.info("是否暂停:" + printerStatus.isPaused);
        while (!printerStatus.isReadyToPrint || printerStatus.isReceiveBufferFull) {
            log.info("打印机暂时无法正常打印，暂停打印中...." + LocalDateTime.now().toString());
            Thread.sleep(2000);
        }
    }

    /**
     *
     * @param zebraPrinter
     * @param qrCode 二维码字符串内容，我是的
     * @param w
     * @param h
     * @throws Exception
     */
    public static void fun2(ZebraPrinter zebraPrinter,String qrCode,int w,int h) throws  Exception{
        log.info("解析二维码图片base64");
        InputStream inputStream = BaseToInputStream(qrCode);
        ZebraImageI image = ZebraImageFactory.getImage(inputStream);
        log.info("开始打印二维码图片");
        zebraPrinter.printImage(image, 0, 20, w, h, false);
    }

     //**BASE64转义**
    public static InputStream BaseToInputStream(String base64string) {
        BASE64Decoder decoder = new BASE64Decoder();
        ByteArrayInputStream stream = null;
        byte[] bytes1 = new byte[0];
        try {
            bytes1 = decoder.decodeBuffer(base64string);
        } catch (IOException e) {
            e.printStackTrace();
        }
        stream = new ByteArrayInputStream(bytes1);
        return stream;
    }

}
